import { AssetContainer, Camera, Scene, SceneLoader } from '@babylonjs/core'
import { GLTFFileLoader } from '@babylonjs/loaders'

class Loader {
  scene: Scene
  container: AssetContainer
  camera: Camera

  constructor(scene: Scene) {
    this.scene = scene
  }

  public async use(url: string) {
    SceneLoader.OnPluginActivatedObservable.addOnce((loader) => {
      if (loader.name === 'gltf') {
        ;(<GLTFFileLoader>loader).onCameraLoadedObservable.addOnce((camera) => {
          this.camera = camera
        })
      }
    })
    this.container = await SceneLoader.LoadAssetContainerAsync(
      url,
      undefined,
      this.scene
    )
    // console.log(this.container)
    return this.container
  }
}

export default Loader
